package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WeitenHong
 * @date 2020/9/30 13:59
 */

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    AddressMapper addressMapper;

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Autowired
    FeedbackMapper feedbackMapper;

    @Override
    public ListBean queryUserListBean(String username, String mobile, Integer page, Integer limit, String sort, String order) {
        //分页 → 在预编译过程中会自动增加上偏移量的sql语句
        PageHelper.startPage(page,limit);

        //example是提供条件的
        UserExample userExample = new UserExample();
        //and + 列名 + 条件
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username != null){
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null){
            criteria.andMobileLike("%" + mobile+"%");
        }
        //criteria.andUsernameLike("%" + username + "%").andMobileEqualTo(mobile);
        //排序
        userExample.setOrderByClause(sort + " " + order);
        List<User> users = userMapper.selectByExample(userExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(users);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(users);
        //listBean.setTotal(users.size());
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public DashBoard getDashBoard() {
        Integer orderTotal = userMapper.orderTotal();
        Integer goodsTotal=userMapper.goodsTotal();
        Integer productTotal=userMapper.productTotal();
        Integer userTotal=userMapper.userTotal();
        DashBoard dashBoard = new DashBoard();
        dashBoard.setUserTotal(userTotal);
        dashBoard.setProductTotal(productTotal);
        dashBoard.setOrderTotal(orderTotal);
        dashBoard.setGoodsTotal(goodsTotal);
        return dashBoard;
    }

    @Override
    public ListBean queryAddressListBean(String name, Integer userId, Integer page, Integer limit, String sort, String order) {
        //分页 → 在预编译过程中会自动增加上偏移量的sql语句
        PageHelper.startPage(page,limit);

        //example是提供条件的
        AddressExample addressExample = new AddressExample();
        //and + 列名 + 条件
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (name != null){
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }

        //排序
        addressExample.setOrderByClause(sort + " " + order);
        List<Address> addresses = addressMapper.selectByExample(addressExample);

        //分页信息
        PageInfo pageInfo = new PageInfo(addresses);
        long total = pageInfo.getTotal();

        ListBean listBean = new ListBean();
        listBean.setItems(addresses);
        listBean.setTotal(total);
        return listBean;
    }

    @Override
    public CollectListBean queryAdminCollectListBean(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        //分页
        PageHelper.startPage(page, limit);
        //使用example提供条件
        CollectExample collectExample = new CollectExample();
        //add + 列名 + 条件
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        //排序
        collectExample.setOrderByClause(sort + " " + order);
        List<Collect> collects = collectMapper.selectByExample(collectExample);
        //分页信息
        PageInfo pageInfo = new PageInfo(collects);
        long total = pageInfo.getTotal();

        CollectListBean collectListBean = new CollectListBean();
        collectListBean.setTotal(total);
        collectListBean.setItems(collects);

        return collectListBean;
    }

    @Override
    public FootprintListBean queryFootprintListBean(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        //分页
        PageHelper.startPage(page, limit);
        //使用example提供条件
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(goodsId != null){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        //排序
        footprintExample.setOrderByClause(sort + " " + order);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        //分页信息
        PageInfo<Footprint> pageInfo = new PageInfo<>(footprints);
        long total = pageInfo.getTotal();
        FootprintListBean footprintListBean = new FootprintListBean();
        footprintListBean.setItems(footprints);
        footprintListBean.setTotal(total);
        return footprintListBean;
    }

    @Override
    public SearchHistoryListBean queryHistoryListBean(Integer page, Integer limit, String keyword, Integer userId, String sort, String order) {
        //分页
        PageHelper.startPage(page, limit);
        //提供条件
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if(userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if(keyword != null){
            criteria.andKeywordLike("%"+ keyword + "%");
        }
        //排序
        searchHistoryExample.setOrderByClause(sort + " " + order);
        //分页信息
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> pageInfo = new PageInfo<>(searchHistories);
        long total = pageInfo.getTotal();

        SearchHistoryListBean searchHistoryListBean = new SearchHistoryListBean();
        searchHistoryListBean.setTotal(total);
        searchHistoryListBean.setItems(searchHistories);
        return searchHistoryListBean;

    }

    @Override
    public FeedbackListBean queryFeedbackListBean(Integer page, Integer limit, Integer id, String username, String sort, String order) {
        //分页
        PageHelper.startPage(page, limit);
        //获得条件
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if(username != null){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(id != null){
            criteria.andIdEqualTo(id);
        }
        //排序
        feedbackExample.setOrderByClause(sort + " " + order);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        //分页信息
        PageInfo pageInfo = new PageInfo(feedbacks);
        long total = pageInfo.getTotal();
        FeedbackListBean feedbackListBean = new FeedbackListBean();
        feedbackListBean.setTotal(total);
        feedbackListBean.setItems(feedbacks);
        return feedbackListBean;
    }

//    @Override
//    public FootprintListBean queryFootprintListBean1(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
//        FootprintExample footprintExample = new FootprintExample();
//        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
//        FootprintListBean footprintListBean = new FootprintListBean();
//        footprintListBean.setItems(footprints);
//        footprintListBean.setTotal((long)44444);
//        return footprintListBean;
//    }


}
